package br.ufpe.cin.mouse.pizza.model;

import java.util.ArrayList;
import java.util.List;

import br.ufpe.cin.mouse.pizza.Util;
import br.ufpe.cin.mouse.pizza.pojo.CriterioPesquisa;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query.FilterOperator;

/**
 * This class defines the methods for basic operations of create, update &
 * retrieve
 * 
 * @author
 * 
 */
public class Item {

	/**
	 * Create or update Item for a particular product. Product has one to many
	 * relation-ship
	 * 
	 * @param productName
	 *            : product name for which the item is created.
	 * @param itemName
	 *            : item name
	 * @param price
	 *            : price of the item
	 * @return
	 */
	public static Entity createOrUpdateItem(String productName,
			String itemName, String price) {
		Entity product = Product.getProduct(productName);
		Entity item = getSingleItem(itemName, productName);
		if (item == null) {
			item = new Entity("Item", product.getKey());
			item.setProperty("name", itemName);
			item.setProperty("product", productName);
			item.setProperty("price", price);
		} else {
			if (price != null && !"".equals(price)) {
				item.setProperty("price", price);
			}
		}
		Util.persistEntity(item);
		return item;
	}

	/**
	 * get All the items in the list
	 * 
	 * @param kind
	 *            : item kind
	 * @return all the items
	 */
	public static Iterable<Entity> getAllItems() {
		Iterable<Entity> entities = Util.listEntities("Item", null, null);
		return entities;
	}

	/**
	 * Get the item given the name
	 * 
	 * @param itemName
	 *            : item name
	 * @return Item Entity
	 */
	public static Iterable<Entity> getItem(String itemName) {
		Iterable<Entity> entities = Util.listEntities("Item", "name", itemName);
		return entities;
	}

	/**
	 * Get all the items for a product
	 * 
	 * @param kind
	 *            : item kind
	 * @param productName
	 *            : product name
	 * @return: all items of type product
	 */
	public static Iterable<Entity> getItemsForProduct(String kind,
			String productName) {
		Key ancestorKey = KeyFactory.createKey("Product", productName);
		return Util.listChildren("Item", ancestorKey);
	}

	/**
	 * get Item with item name
	 * 
	 * @param itemName
	 *            : get itemName
	 * @param product
	 *            : get product
	 * @return item entity
	 */
	public static Entity getSingleItem(String itemName, String product) {

		// Iterable<Entity> results = Util.listEntities("Item", "name",
		// itemName);

		ArrayList<CriterioPesquisa> criterios = new ArrayList<CriterioPesquisa>();
		criterios.add(new CriterioPesquisa("name", itemName,
				FilterOperator.EQUAL));
		criterios.add(new CriterioPesquisa("product", product,
				FilterOperator.EQUAL));

		Iterable<Entity> results = Util.listEntitiesVariosParametros("Item",
				criterios);

		List<Entity> entity = new ArrayList<Entity>();
		for (Entity e : results)
			if (e != null)
				entity.add(e);
		if (!entity.isEmpty()) {
			return (Entity) entity.remove(0);
		}
		return null;
	}
}
